﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Process.BLL;

namespace Business
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CartBLL cart = (CartBLL)Session["cart"];
                if (cart == null)
                {
                    lblMessage.Text = "Chưa có mặt hàng nào trong giỏ hàng";                  
                    cmdContinue.Visible = false;    
                    return;
                }

                if (cart.TotalProduct() == 0)
                {
                    lblMessage.Text = "Chưa có mặt hàng nào trong giỏ hàng";                  

                    return;
                }

                grdCartDetail.DataSource = cart.CreateCart();
                grdCartDetail.DataBind();
                lblMessage.Text = "Giỏ hàng có: " + cart.TotalProduct().ToString()+ " mặt hàng <b>";
              

            }

        }

        protected void cmdContinue_Click(object sender, EventArgs e)
        {
            Response.Redirect("Home.aspx");
        }

        protected void cmdPayment_Click(object sender, EventArgs e)
        {
            Response.Redirect("Order.aspx");
        }

        protected void grdCartDetail_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string cmdName = e.CommandName;
            //cannot cast object e.CommandArgument into Int
            string sIndex = (string)e.CommandArgument;
            int index = int.Parse(sIndex);

            //HidenField(contrain ProductID0
            Label lblProductID = (Label)(grdCartDetail.Rows[index].FindControl("lblProductID"));


            if (cmdName.Equals("Update"))
            {
                //Get control txtQuantity (control type:textbox)
                TextBox txtQuantity = (TextBox)(grdCartDetail.Rows[index].FindControl("txtQuantity"));
                int quantity = int.Parse(txtQuantity.Text);
               
                CartBLL cart = (CartBLL)Session["cart"];
                cart.UpdateProductInCart(lblProductID.Text, quantity);
                grdCartDetail.DataSource = cart.CreateCart();
                grdCartDetail.DataBind();
                lblTotalAmount.Text = "Số Tiền là: " + cart.GetTotalAmount() + " USD <br>";


            }
            else if (cmdName.Equals("Delete"))
            {

             
                CartBLL cart = (CartBLL)Session["cart"];
                cart.RemoveProductByID(lblProductID.Text);
                grdCartDetail.DataSource = cart.CreateCart();
                grdCartDetail.DataBind();

                lblMessage.Text = "Giỏ hàng có: " + cart.TotalProduct() + " mặt hàng <b>";
                lblTotalAmount.Text = "Tổng cộng " + cart.GetTotalAmount() + " USD<br>";

            }
        }

       

        protected void grdCartDetail_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            this.grdCartDetail.DataBind();
        }

        protected void grdCartDetail_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            this.grdCartDetail.DataBind();
        }
    }
}